<?php
	ini_set('date.timezone','Asia/Shanghai');
	include_once 'isLogin.php';
	
	error_reporting(E_ERROR);
	require_once "../wxpay/lib/WxPay.Api.php";  //包括 data ，config
	require_once '../wxpay/example/log.php';

	//初始化日志
	// $logHandler= new CLogFileHandler("../wxlogs/".date('Y-m-d').'.log');
	// $log = Log::Init($logHandler, 15);
	
	$order_id = $_GET['id'];
	$orderc = new order($pagec);
	$where = ' AND o.id = "' . $order_id . '"';
	$row = $orderc->orderInfo($where , 'o.*');
	//退款申请
	$refund = $orderc->refundInfo($order_id , '*' , 'order_id');
	$row['refund_info'] = $refund;
	//$smarty->assign('row',$row);
	$transaction_id = $row['wxpay_code'];			//交易号
	
	if(isset($transaction_id) && $transaction_id != ""){
		$total_fee = $row["pay_money"] * 100;
		$refund_fee = $refund['refund_cash'] * 100;
		$input = new WxPayRefund();
		$input->SetTransaction_id($transaction_id);
		$input->SetTotal_fee($total_fee);
		$input->SetRefund_fee($refund_fee);
	    $input->SetOut_refund_no(WxPayConfig::MCHID.date("YmdHis"));
	    $input->SetOp_user_id(WxPayConfig::MCHID);
		$data = printf_info($orderc , WxPayApi::refund($input) , $order_id , $refund['id']);
		exit();
	}
	
	function printf_info($orderc , $data , $order_id , $refund_id)
	{
		if($data['result_code'] == 'SUCCESS'){
			$data['result_name'] = '退款申请成功，等待微信退款';
			$orderc->orderUpdate(array('pay_status' => '4' , 'trading_status' => '2') , ' id = "'.$order_id.'"');
			$orderc->refundUpdate(array('refund_code' => $data['refund_id']) , ' id ="'. $refund_id .'"');
		}
		return $data;
	}

	$smarty->assign('data',$data);
	$smarty->display('wxpay_refund_result.html'); 
?>